<template>
	<!-- 创建委外订单 -->
	<div>
		<v-dialog v-model="woutOrderCommentModel.dialog" width="900">
			<v-card>
				<v-card-title>创建委外订单</v-card-title>
				<v-card-text>
					<v-container>
						<v-expansion-panels>
							<v-expansion-panel>
								<v-expansion-panel-header>
									维护需委外的子工单信息
								</v-expansion-panel-header>
								<v-expansion-panel-content>
									<v-simple-table dense fixed-header class="text-no-wrap">
										<template v-slot:default>
											<thead>
												<tr>
													<th class="text-center">
														操作
													</th>
													<th class="text-center">
														单据编号
													</th>
													<th class="text-center">
														供应商编码
													</th>
													<th class="text-center">
														供应商名称
													</th>
													<th class="text-center">
														税率
													</th>
													<th class="text-center">
														汇率
													</th>
													<th class="text-center">
														存货编码
													</th>
													<th class="text-center">
														存货名称
													</th>
													<th class="text-center">
														数量
													</th>
													<th class="text-center">
														原币单价
													</th>
													<th class="text-center">
														计划下达日期
													</th>
													<th class="text-center">
														计划到货日期
													</th>
													<th class="text-center">
														税率
													</th>
												</tr>
											</thead>
											<tbody>
												<tr v-for="m in WoutModel.detailList" :key="m.id">
													<td class="text-center">
														<v-btn class="ml-2" small tile color="success"
															@click="editDetailWoutClick(m)">
															编辑
														</v-btn>
														<v-btn class="ml-2" small tile color="red"
															@click="removeWoutClick(m)">
															删除
														</v-btn>
													</td>
													<td class="text-center">{{WoutModel.code}}</td>
													<td class="text-center" :style="temp.cVenCode">
														{{WoutModel.suppliercode}}
													</td>
													<td class="text-center">{{WoutModel.suppliername}}</td>
													<td class="text-center">{{WoutModel.taxrate}}</td>
													<td class="text-center">{{WoutModel.exchange}}</td>

													<td class="text-center">{{m.invcode}}</td>
													<td class="text-center">{{m.invname}}</td>
													<td class="text-center">{{m.quantity}}</td>
													<td class="text-center">{{m.unitprice}}</td>
													<td class="text-center">{{m.scheduleddate}}</td>
													<td class="text-center">{{m.planneddate}}</td>
													<td class="text-center">{{m.taxrate}}</td>

												</tr>
											</tbody>
										</template>
									</v-simple-table>
								</v-expansion-panel-content>
							</v-expansion-panel>
						</v-expansion-panels>
						<br />
						<v-divider></v-divider>
						<br />
						<v-row>

							<v-col cols="4">
								<v-text-field label="单据编号" :style="this.temp" disabled v-model="WoutModel.code" dense
									outlined>
								</v-text-field>
							</v-col>
							<v-col cols="4">
								<v-text-field label="税率" v-model="WoutModel.taxrate" dense outlined>
								</v-text-field>
							</v-col>
							<v-col cols="4">
								<v-text-field label="汇率" v-model="WoutModel.exchange" dense outlined>
								</v-text-field>
							</v-col>
							<v-col cols="12">
								<v-row>
									<v-col cols="12" sm="6" md="5">
										<v-text-field label="供应商编码(必选)" disabled :style="temp.cVenCode"
											v-model="WoutModel.suppliercode" dense outlined>
										</v-text-field>
									</v-col>
									<v-col cols="12" sm="6" md="5">
										<v-text-field label="供应商名称(必选)" disabled :style="temp.cVenCode"
											v-model="WoutModel.suppliername" dense outlined>
										</v-text-field>
									</v-col>
									<v-col cols="12" sm="6" md="2">
										<v-btn class="mt-1" small tile color="primary" @click="selectSupplierClick">
											选择供应商</v-btn>
									</v-col>
								</v-row>
							</v-col>
							<v-col cols="12">
								<v-divider></v-divider>
							</v-col>

							<v-col cols="4">
								<v-text-field label="委外数量（必填）" v-model="WoutDetail.quantity" dense outlined>
								</v-text-field>
							</v-col>
							<v-col cols="4">
								<v-text-field label="原币单价（必填）" v-model="WoutDetail.unitprice" dense outlined>
								</v-text-field>
							</v-col>
							<v-col cols="4">
								<v-text-field label=" 税率（必填）" v-model="WoutDetail.taxrate" dense outlined>
								</v-text-field>
							</v-col>
							<v-col cols="4">
								<v-menu v-model="scheduleddateMenu" :close-on-content-click="false" :nudge-right="40"
									transition="scale-transition" offset-y min-width="auto">
									<template v-slot:activator="{ on, attrs }">
										<v-text-field required v-model="WoutDetail.scheduleddate" label="计划下达日期（必填）"
											readonly v-bind="attrs" v-on="on" dense outlined>
										</v-text-field>
									</template>
									<v-date-picker locale="zh-cn" v-model="WoutDetail.scheduleddate"
										@input="scheduleddateMenu = false">
									</v-date-picker>
								</v-menu>

							</v-col>
							<v-col cols="4">
								<v-menu v-model="planneddateMenu" :close-on-content-click="false" :nudge-right="40"
									transition="scale-transition" offset-y min-width="auto">
									<template v-slot:activator="{ on, attrs }">
										<v-text-field required v-model="WoutDetail.planneddate" label="计划到货日期(必填)"
											readonly v-bind="attrs" v-on="on" dense outlined>
										</v-text-field>
									</template>
									<v-date-picker locale="zh-cn" v-model="WoutDetail.planneddate"
										@input="planneddateMenu = false">
									</v-date-picker>
								</v-menu>
							</v-col>
						</v-row>
					</v-container>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="success" class="mr-4" text @click="wOutOrderSubmit">提交</v-btn>
					<v-btn color="red" class="mr-4" text @click="closeCreateWout">关闭</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 供应商对话框 -->
		<suppliercomment :supplierCommentModel="supplierCommentModel" :callBack="setSupplierSelectCallBack">
		</suppliercomment>
	</div>
</template>

<script>
	import suppliercomment from '../../../pdm/components/SupplierDataList.vue'
	export default {
		name: 'wout_order_created',
		components: {
			suppliercomment
		},
		props: {
			callBack: {
				type: Function, //参数类型：函数
			},
			woutOrderCommentModel: {
				dialog: false,
				//委外子工单号
				cards: []
			},
		},
		data: () => ({
			order: {},
			supplierCommentModel: {
				dialog: false,
				loadSupplierUrl: `MES/MESPostingWO/LoadSupplierPage`
			},
			WoutModel: {
				detailList: [],
				taxrate: '13',
				exchange: '1'
			},
			WoutDetail: {},
			temp: {},
			scheduleddateMenu: false,
			planneddateMenu: false,
		}),
		watch: {
			woutOrderCommentModel: {
				handler(newVal) {
					this.$emit('update:woutOrderCommentModel', newVal)
					this.loadWoutCode()
					this.loadWoutOrder()
				},
				deep: true
			},
		},

		methods: {
			selectSupplierClick() {
				this.supplierCommentModel.dialog = true
			},
			setSupplierSelectCallBack(supplier) {
				this.temp = supplier
				this.WoutModel.suppliercode = supplier.cVenCode
				this.WoutModel.suppliername = supplier.cVenName
			},
			//获取委外单据号
			async loadWoutCode() {
				var response = await this.$httpRequest({
					url: `Common/GetSequenceNoCode?orderType=90&length=8`,
					method: 'get'
				})
				if (response.status == 1) {
					this.temp = response.data
					this.WoutModel.code = response.data
				} else {
					this.$message.error(response.info)
				}
			},

			async loadWoutOrder() {
				this.WoutModel.detailList = []
				this.woutOrderCommentModel.cards.forEach(r => {
					var detail = {};
					detail.invcode = r.childrenOrder.cinvocde
					detail.invname = r.childrenOrder.name
					detail.quantity = r.childrenOrder.totalcount
					detail.unitprice = 0
					detail.taxrate = 13
					detail.projectcode = r.childrenOrder.projectcode
					detail.projectname = r.childrenOrder.projectname
					detail.masterordrid = r.childrenOrder.masterid
					detail.childrenorderid = r.childrenOrder.id
					detail.scheduleddate = this.getCurrentDate()
					detail.planneddate = this.getCurrentDate()
					detail.woutcode = this.WoutModel.code
					this.WoutModel.detailList.push(detail)
				})

			},
			//计划下达日期、计划到货日期默认都是加一个月
			getCurrentDate() {
				const dt = new Date();
				const y = dt.getFullYear();
				dt.setMonth((new Date().getMonth() + 1));
				const m = (dt.getMonth() + 1 + '').padStart(2, '0');
				const d = (dt.getDate() + '').padStart(2, '0');
				return `${y}-${m}-${d}`;
			},
			//编辑明细
			editDetailWoutClick(detail) {
				this.WoutDetail = detail
			},
			//移除明细
			removeWoutClick(detail) {
				this.WoutModel.detailList.splice(detail, 1)
			},
			closeCreateWout() {
				this.woutOrderCommentModel.dialog = false
				this.WoutModel.detailList = []
				this.WoutDetail = {}
			},
			//创建委外订单
			async wOutOrderSubmit() {
				var response = await this.$httpRequest({
					url: `MES/MESPostingWO/CustomCreateWOOrder`,
					method: 'post',
					data: this.WoutModel
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					this.$props.callBack()
					this.closeCreateWout()
				} else {
					this.$message.error(response.info)
				}
			},





		}

	}
</script>

<style>
</style>
